﻿@using ComputerLock.Resources
@using System.Text
@using JiuLing.CommonLibs.ExtensionMethods
@inject IStringLocalizer<Lang> Lang
@inject AppSettings AppSettings
@inject ISnackbar Snackbar
<MudDialog>
    <DialogContent>
        <MudTextField @bind-Value="_currentPassword"
                      Label="@(Lang["CurrentPassword"])"
                      InputType="InputType.Password"
                      Variant="Variant.Text" />

        <MudTextField @bind-Value="_newPassword"
                      Label="@(Lang["NewPassword"])"
                      InputType="InputType.Password"
                      Variant="Variant.Text" />

        <MudTextField @bind-Value="_confirmPassword"
                      Label="@(Lang["ConfirmPassword"])"
                      InputType="InputType.Password"
                      Variant="Variant.Text" />
    </DialogContent>
    <DialogActions>
        <MudButton Color="Color.Primary"
                   OnClick="Submit">
            @(Lang["Save"])
        </MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] MudDialogInstance MudDialog { get; set; }

    private string _currentPassword;
    private string _newPassword;
    private string _confirmPassword;
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
    }


    private void Submit()
    {
        if (_newPassword.IsEmpty())
        {
            Snackbar.Add(Lang["PasswordEmpty"], Severity.Error);
            return;
        }

        if (_newPassword != _confirmPassword)
        {
            Snackbar.Add(Lang["PasswordInconsistent"], Severity.Error);
            return;
        }

        if (AppSettings.Password != JiuLing.CommonLibs.Security.MD5Utils.GetStringValueToLower(_currentPassword))
        {

            Snackbar.Add(Lang["WrongPassword"], Severity.Error);

            return;
        }

        var newPassword = JiuLing.CommonLibs.Security.MD5Utils.GetStringValueToLower(_newPassword);
        MudDialog.Close(DialogResult.Ok(newPassword));
        Snackbar.Add(Lang["SaveOk"], Severity.Success);
    }
}